﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using QLBH.Models;


namespace QLBH.Utilities
{
    public class WSAccess
    {
        private static String baseUrl = "http://54.186.149.52/saleserver/public/";
        /// <summary>
        /// Send POST request to server to insert data
        /// </summary>
        /// <typeparam name="T">Type of object input</typeparam>
        /// <typeparam name="C">object type want return value</typeparam>
        /// <param name="obj">object input</param>
        /// <param name="url">the server api address</param>
        /// <returns>C object </returns>
        public static C Insert<T,C>(T obj,String url) 
            where T: ModelBase 
            where C : class
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    String srvurl = baseUrl + url;
                    NameValueCollection postData = obj.PrepareValue();

                    // client.UploadValues returns page's source as byte array (byte[])
                    // so it must be transformed into a string
                    string pagesource = Encoding.UTF8.GetString(client.UploadValues(srvurl, "POST", postData));

                    return JsonConvert.DeserializeObject<C>(pagesource);
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// Get all data from server with GET method
        /// </summary>
        /// <typeparam name="T">Class type want sync and return</typeparam>
        /// <param name="url">Addess of server API</param>
        /// <returns>T object</returns>
        public static T GetData<T>(String url) where T : class
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    String srvurl = baseUrl + url;
                    // this string contains the webpage's source
                    string pagesource = client.DownloadString(srvurl);

                    return JsonConvert.DeserializeObject<T>(pagesource);
                }
            }
            catch(Exception e)
            {
                throw e;
            }
            
        }
        /// <summary>
        /// Send Post request to server to run Update object
        /// </summary>
        /// <typeparam name="T">Class type of object model want update on server</typeparam>
        /// <param name="obj">object</param>
        /// <param name="url">path to Server API</param>
        /// <returns></returns>
        public static ResponseBase Update<T>(T obj,String url) where T: ModelBase
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    String srvurl = baseUrl + url;
                    NameValueCollection postData = obj.PrepareValue();

                    // client.UploadValues returns page's source as byte array (byte[])
                    // so it must be transformed into a string
                    string pagesource = Encoding.UTF8.GetString(client.UploadValues(srvurl, "POST", postData));

                    return JsonConvert.DeserializeObject<ResponseBase>(pagesource);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            
        }
        /// <summary>
        /// send post request to server to delete object
        /// </summary>
        /// <param name="url"> URL to Server API</param>
        /// <returns></returns>
        public static ResponseBase Delete<T>(String url,T obj) where T: ModelBase
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    String srvurl = baseUrl + url;
                    NameValueCollection postData = obj.PrepareValue();
                    //postData.Add("id", id);
                    string pagesource = Encoding.UTF8.GetString(client.UploadValues(srvurl, "POST", postData));
                    return JsonConvert.DeserializeObject<ResponseBase>(pagesource);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }    
}
